<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父类id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父类id"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="departName">
      <el-input v-model="dataForm.departName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="类别" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="类别"></el-input>
    </el-form-item>
    <el-form-item label="等级" prop="depth">
      <el-input v-model="dataForm.depth" placeholder="等级"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="childCount">
      <el-input v-model="dataForm.childCount" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="部门描述" prop="departDescribe">
      <el-input v-model="dataForm.departDescribe" placeholder="部门描述"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="modifyBy">
      <el-input v-model="dataForm.modifyBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="公司代号" prop="corpCode">
      <el-input v-model="dataForm.corpCode" placeholder="公司代号"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="deleted">
      <el-input v-model="dataForm.deleted" placeholder="逻辑删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          departName: '',
          sort: '',
          depth: '',
          childCount: '',
          departDescribe: '',
          createBy: '',
          createTime: '',
          modifyBy: '',
          modifyTime: '',
          corpCode: '',
          version: '',
          deleted: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '父类id不能为空', trigger: 'blur' }
          ],
          departName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '类别不能为空', trigger: 'blur' }
          ],
          depth: [
            { required: true, message: '等级不能为空', trigger: 'blur' }
          ],
          childCount: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          departDescribe: [
            { required: true, message: '部门描述不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          corpCode: [
            { required: true, message: '公司代号不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/dep/rdepartment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.rDepartment.parentId
                this.dataForm.departName = data.rDepartment.departName
                this.dataForm.sort = data.rDepartment.sort
                this.dataForm.depth = data.rDepartment.depth
                this.dataForm.childCount = data.rDepartment.childCount
                this.dataForm.departDescribe = data.rDepartment.departDescribe
                this.dataForm.createBy = data.rDepartment.createBy
                this.dataForm.createTime = data.rDepartment.createTime
                this.dataForm.modifyBy = data.rDepartment.modifyBy
                this.dataForm.modifyTime = data.rDepartment.modifyTime
                this.dataForm.corpCode = data.rDepartment.corpCode
                this.dataForm.version = data.rDepartment.version
                this.dataForm.deleted = data.rDepartment.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/dep/rdepartment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'departName': this.dataForm.departName,
                'sort': this.dataForm.sort,
                'depth': this.dataForm.depth,
                'childCount': this.dataForm.childCount,
                'departDescribe': this.dataForm.departDescribe,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'modifyBy': this.dataForm.modifyBy,
                'modifyTime': this.dataForm.modifyTime,
                'corpCode': this.dataForm.corpCode,
                'version': this.dataForm.version,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
